<?php
namespace app\cms\controller;
use framework\ext\Check;
/* *
 * 数据库表与字段管理功能
 * */
class ModuleController extends \app\base\controller\AdminController{

    //管理模型表
    public function index(){
        $act = form('get.act');
        if($act == 'del'){
            $fid = form('get.id',0,intval);
            $result = model('module')->_delete($fid);
            if(!$result){
                $this->jsonMsg('删除成功',1);
            }else{
                $this->jsonMsg($result);
            }
        }else{
            $page = form('get.page',0,intval);
            $this->list = model('module')->pager($page,20)->_select(array('locks' => 0),'id desc'); 
            $this->pager = $this->getPage(model('module')->pager);
            $this->display(); 
        }
    }
    //添加模型表
    public function add(){
        if($this->isPost()){
            $msg = Check::rule(
                    array(check::must($_POST['name']),'模型名称必须填写'),
                    array(check::must($_POST['tables']),'模型表名必须填写'),
                    array(check::userName($_POST['tables'],3,20,'EN'),'只能输入3至20个纯英文字母'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $data = array();
            $data['name']   = form('post.name'); //模型名称
            $data['type']  = form('post.type',1,intval);
            $data['tables']  = 'cms_ext_'.form('post.tables'); //表名
            $data['uptime'] = time(); 
            $result = model('module')->addModule($data);
            if($result){
                $this->jsonMsg('模型添加成功',1,url('module/index'));
            }else{
                $this->jsonMsg('模型添加失败');
            }
        }else{
            $this->display();
        }
    }
    //编辑模型表
    public function edit(){
        if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['name']),'模型名称必须填写'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $data = array();
            $data['name'] = form('post.name');
            $data['uptime'] = time();
            $fid = form('post.fid',0,intval);
            $result = model('module')->editModule($fid,$data);
            if($result){
                $this->jsonMsg('模型修改成功',1,url('module/index'));
            }else{
                $this->jsonMsg('模型修改失败');
            }
        }else{
            $fid = form('get.id',0,intval);
            $this->info = model('module')->_find(array('id' =>$fid),'id,name');
            $this->display(); 
        }
    }
    
    //判断模型是否存在
    public function modulename(){
        $name = form('post.name');
        $param = form('post.param');
        if(!$name || !$param){
            $info['info']= '模型表名不能空！';
            $info['status']= 'n';
            echo json_encode($info);
        }else{
            $result = model('module')->_find(array('tables'=>'cms_ext_'.$param),'tables');
            if($result){
                $info['info']= '相同表已经存在,请更换其它!';
                $info['status']= 'n';
                echo json_encode($info);
            }else{
                $info['info']= '可以使用';
                $info['status']= 'y';
                echo json_encode($info);
            }  
        }
    }

    //################################################
    //管理模型字段
    public function field(){
        $act = form('get.act');
        if($act == 'del'){
            $fid = form('get.id',0,intval);
            $result = model('field')->delField($fid);
            if(!$result){
                $this->jsonMsg('删除成功',1);
            }else{
                $this->jsonMsg($result);
            }
        }else{
            $page = form('get.page',0,intval);
            $mid = form('get.id',0,intval);
            $this->info = model('module')->_find(array('id' =>$mid),'id,name'); 
            $this->list = model('field')->pager($page,20)->_select(array('mid' =>$mid),'neworder asc,id asc');
            $this->pager = $this->getPage(model('field')->pager,array(id=>$mid));
            $this->display();  
        }
    }

    //添加模型字段
    public function fieldadd(){
        if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['mid']),'模型ID不能为空'),
                    array(check::must($_POST['name']),'字段标识不能为空'),
                    array(check::must($_POST['fields']),'字段名不能为空'),
                    array(check::userName($_POST['fields'],3,20,'EN'),'只能输入3至20个纯英文字母'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $mid = form('post.mid',0,intval);
            $data = array();
            $data = form('post',array('fid','fields'));
            $data['fields'] = 'ext_'.form('post.fields');
            $data['uptime'] = time();
            $result = model('field')->addField($data);
            if($result){
                $this->jsonMsg('字段添加成功',1,url('module/field',array('id'=>$mid)));  
            }else{
                $this->jsonMsg('字段添加失败,查看是否添加相同字段'); 
            }
        }else{
            $this->pid = form('get.id',0,intval);
            $this->tables = model('module')->_find(array('id'=>$this->pid),'id,name'); 
            $this->display();
        }
    }
    
    //管理模型字段
    public function fieldedit(){
        if($this->isPost()){
            $data = array();
            $data = form('post',array('fid','fields'));
            $fid = form('post.fid',0,intval);
            $mid = form('post.mid',0,intval);
            $result = model('field')->fieldedit($fid,$data);
            if($result){
                $this->jsonMsg('字段信息修改成功',1,url('module/field',array('id'=>$mid)));
            }else{
                $this->jsonMsg('字段修改失败');
            }
        }else{
            $this->fid= form('get.id',0,intval);
            $this->info = model('field')->_find(array('id'=>$this->fid));
            if($this->info){
                $str = explode('_',$this->info['fields']);
                $this->fields = $str[1];
            }
            $this->tables = model('module')->_find(array('id'=>$this->info['mid']),'id,name'); 
            $this->display();   
        }
    } 
    
    //字段排序
    public function uporter(){
        $fid=  intval($_POST['id']);
        $condition['id'] = $fid;
        $data['neworder'] = intval($_POST['sorter']);
        $info = model('field')->_update($data,$condition);
        if($info){
            $this->jsonMsg('模型字段自定义排序成功',1);
        }else{
            $this->jsonMsg('模型字段自定义排序失败');
        }
    }
}